package com.koron.sinoprobe.web.engineering.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.*;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpDivision;
import com.koron.sinoprobe.web.engineering.bean.po.SpSettlementAudit;
import com.koron.sinoprobe.web.engineering.bean.query.SpSettlementAuditQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpSettlementAuditVO;
import com.koron.sinoprobe.web.engineering.service.SpSettlementAuditService;
import com.koron.sinoprobe.web.engineering.workflow.StaffHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 结算审查(SpSettlementAudit)表控制层
 *
 * @author jzp
 * @since 2021-12-02 10:00:02
 */
@Api(tags = "结算审查接口")
@RestController
@RequestMapping("sp/settlementAudit")
public class SpSettlementAuditController implements WorkflowController, WorkflowPluginController {
    /**
     * 服务对象
     */
    @Autowired
    private SpSettlementAuditService spSettlementAuditService;
    @Autowired
    private StaffHelper staffHelper;
    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<SpSettlementAuditVO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "getById", SpSettlementAuditVO.class, id));
    }

    @ApiOperation(value = "根据项目id查询")
    @GetMapping("getByProjectId")
    public Response<SpDivision> selectOneByProjectId(String projectId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "getByProjectId", SpDivision.class, projectId));
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<SpSettlementAuditVO>> page(PageQuery pageQuery, SpSettlementAuditQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "查询已被结算审查的项目id集合")
    @GetMapping("related")
    public Response<List<String>> related() {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "getRelated", List.class));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody SpSettlementAudit spSettlementAudit) {
        ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "saveOrUpdate", Boolean.class, spSettlementAudit);
        return Response.ok(spSettlementAudit.getId());
    }

    @ApiOperation(value = "提交")
    @PostMapping("submit")
    public Response<String> submit(@RequestBody SpSettlementAudit spSettlementAudit) {
        ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "submit", Boolean.class, spSettlementAudit);
        return Response.ok(spSettlementAudit.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    /**
     * 工作溜
     */
    @Override
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        // 提交
        if (redirectQuery.getOperation() == 32) {
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "end", Boolean.class, redirectQuery.getDataId());
            } else {
                // 否则为提交
                ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "updateStatus", Boolean.class, redirectQuery.getDataId(), Constants.AppceptStatus.WAIT_REVIEW);
            }
        } else if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            ADOConnection.runTask(getCurrDs(), spSettlementAuditService, "updateStatus", Boolean.class, redirectQuery.getDataId(), Constants.PackageStatus.DRAFT);
        }
        return Response.success();
    }


    /**
     * 取交集
     */
    @Override
    public Response<List<WorkflowUser>> submitter(@RequestBody SubmitterParam submitterParam) {
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

    @Override
    public Response<List<WorkflowUser>> handler(@RequestBody SubmitterParam submitterParam) {
        return Response.ok();
    }

    /**
     * 添加编制人所在部门
     */
    @PostMapping("creatorOrg/"+submitter)
    public Response<List<WorkflowUser>> creatorOrgSubmitter(@RequestBody SubmitterParam submitterParam) {
        // 1. 获取编制人信息
        SpSettlementAudit spSettlementAudit = JSONObject.parseObject(submitterParam.getFormVariables(), SpSettlementAudit.class);
        staffHelper.fillAuthorizerOrg(submitterParam, spSettlementAudit.getCreateBy());
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

}
